
const webpack = require('webpack-stream')
const sass    = require('gulp-sass')
const cleanCss= require('gulp-clean-css')
const htmlmin = require('gulp-htmlmin')
const rev     = require('gulp-rev')
const revCollector = require('gulp-rev-collector')
const del     = require('del')  

const{src,series,dest,parallel} = require("gulp")



function packHtml(){
    return src(['../src/*.html','../build/rev/**/*.json'])
        .pipe(revCollector())
        .pipe(htmlmin({ collapseWhitespace: true }))
        .pipe(dest('../build'))
}


function packAssets(){
  return src('../src/assets/**/*')
      .pipe(dest('../build/assets'))
      
}
// 拷贝字符串模板插件
function packLibs(){
  return src('../src/libs/**/*')
      .pipe(dest('../build/libs'))
      
}


function packCss(){
    return src('../src/styles/main.scss')
        .pipe(sass().on('error',sass.logError))
        .pipe(cleanCss({compatibility: 'ie8'}))
        .pipe(rev())
        .pipe(dest('../build/styles'))
        .pipe(rev.manifest())
        .pipe(dest('../build/rev/styles'))
        
}


function packJs(){
    return src('../src/scripts/main.js')
        .pipe(webpack({
            mode:'production',
            entry:'../src/scripts/main.js',
            output :{
                filename:'main.js'
            },
            module: {
                rules: [
                  {
                    test: /\.m?js$/,
                    exclude: /(node_modules|bower_components)/,
                    use: {
                      loader: 'babel-loader',
                      options: {
                        presets: ['@babel/preset-env'],
                        plugins: ['@babel/plugin-transform-runtime']
                      }
                    }
                  },
                  {
                    test: /\.html$/,
                    loader: 'string-loader'
                  }
                ]
              }
        }))
        .pipe(rev())
        .pipe(dest('../build/scripts'))
        .pipe(rev.manifest())
        .pipe(dest('../build/rev/scripts'))
        
}


function delDevFile(){
    return del('../build/**', {
        force: true
      })
}





exports.default = series(delDevFile,packCss,packJs,parallel(packHtml,packAssets,packLibs));